package com.caiyouxi.goods.service;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.goods.mapper.GoodsPropertiesConfigMapper;
import com.caiyouxi.goods.model.GoodsPropertiesConfig;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/11/9.
 */
@Service
public class GoodsPropertiesConfigService extends BaseService {

    private Logger logger = Logger.getLogger(GoodsPropertiesConfigService.class);

    @Autowired
    private GoodsPropertiesConfigMapper goodsPropertiesConfigMapper;

    /**
     * 查询商品或订单的扩展属性配置
     */
    public ResData get_goods_properties_config_info(GoodsPropertiesConfig goodsPropertiesConfig){
        String redis_key = "GOODS_PROPERTIES_CONFIG_"+goodsPropertiesConfig.getId();
        logger.info("search redis...");
        GoodsPropertiesConfig newGoodsPropertiesConfig = (GoodsPropertiesConfig) redisService.get(redis_key);
        if (null == newGoodsPropertiesConfig){
            logger.info("search db...");
            newGoodsPropertiesConfig  =goodsPropertiesConfigMapper.select_goods_properties_config(goodsPropertiesConfig.getId());
            redisService.set(redis_key,newGoodsPropertiesConfig);
        }
        return  success(newGoodsPropertiesConfig);
    }

    /**
     * 查询商品或订单的扩展属性配置列表
     */
    public ResData get_goods_properties_config_list(GoodsPropertiesConfig goodsPropertiesConfig){
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            goodsPropertiesConfig.setPage_size(reids_page_size);
        }

        HashMap<String, Object> map = new HashMap();
        map.put("start",goodsPropertiesConfig.getStart());
        map.put("page_size",goodsPropertiesConfig.getPage_size());
        map.put("category_id",goodsPropertiesConfig.getCategory_id());
        map.put("apply_to",goodsPropertiesConfig.getApply_to());
        List<GoodsPropertiesConfig> goodsDiscounts = goodsPropertiesConfigMapper.select_goods_properties_config_by_category_id_and_apply_to(map);
        int total = goodsPropertiesConfigMapper.select_goods_properties_config_count(goodsPropertiesConfig);
        QueryParams pageList = new QueryParams();
        pageList.setTotal(total);
        pageList.setPage(goodsPropertiesConfig.getPage());
        pageList.setPage_size(goodsPropertiesConfig.getPage_size());
        pageList.setList(goodsDiscounts);
        return success(pageList);
    }

    /**
     * 新增商品或订单的扩展属性配置
     */
    @Transactional
    public ResData add_goods_properties_config(GoodsPropertiesConfig goodsPropertiesConfig){
        int result =goodsPropertiesConfigMapper.add_goods_properties_config(goodsPropertiesConfig);
        if(result == 1){
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCONFIG_ADD_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCONFIG_ADD_ERROR.getMsg(),null);
    }

    /**
     * 修改商品或订单的扩展属性配置
     */
    @Transactional
    public ResData update_goods_properties_config(GoodsPropertiesConfig goodsPropertiesConfig){
        int result = goodsPropertiesConfigMapper.update_goods_properties_config(goodsPropertiesConfig);
        if(result == 1){
            String redis_key = "GOODS_PROPERTIES_CONFIG_"+goodsPropertiesConfig.getId();
            redisService.set(redis_key,goodsPropertiesConfig);  //更新缓存内容
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCONFIG_UPDATE_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCONFIG_UPDATE_ERROR.getMsg(),null);
    }

    /**
     * 删除商品或订单的扩展属性配置
     */
    @Transactional
    public ResData delete_goods_properties_config(GoodsPropertiesConfig goodsPropertiesConfig){
        int result =goodsPropertiesConfigMapper.delete_goods_properties_config(goodsPropertiesConfig.getId());
        if(result == 1){
            String redis_key = "GOODS_PROPERTIES_CONFIG_"+goodsPropertiesConfig.getId();
            redisService.remove(redis_key);  //删除缓存
            return success(null);
        }
        return failed(ErrorCode.SYS_GOOdSPROPERTIESCONFIG_DELTET_ERROR.getCode(),ErrorCode.SYS_GOOdSPROPERTIESCONFIG_DELTET_ERROR.getMsg(),null);

    }





}
